package trackexplorer.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import trackexplorer.editors.MapViewer;

public class ShowView extends ViewPart{
    public static final String ID        = "trackexplorer.views.ShowView";
    private Canvas             canvas;
    private double             scale;
    private Rectangle          rect;
    private boolean            mouseDown = false;

    public ShowView(){}

    public void setMiniMap(Image miniMap, double scale){
        this.scale = scale;
        Image tmp = canvas.getBackgroundImage();
        if(tmp != null && !tmp.isDisposed()){
            tmp.dispose();
            tmp = null;
        }
        Rectangle bounds = miniMap.getBounds();
        canvas.setSize(bounds.width, bounds.height);
        canvas.setBackgroundImage(miniMap);
    }

    public void mapPartListener(double x, double y, double width, double height){
        rect = new Rectangle((int)(x / scale), (int)(y / scale),
            (int)(width / scale), (int)(height / scale));
        canvas.redraw();
    }

    @Override
    public void createPartControl(Composite parent){
        parent.setLayout(new FillLayout());
        ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL
            | SWT.H_SCROLL);
        canvas = new Canvas(sc, SWT.NONE);
        sc.setBackground(new Color(parent.getDisplay(), 255, 255, 255));
        sc.setContent(canvas);
        canvas.addPaintListener(new PaintListener(){

            public void paintControl(PaintEvent e){
                IWorkbenchPage activePage = PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow().getActivePage();
                if(activePage == null){
                    return;
                }
                IEditorPart activeEditor = activePage.getActiveEditor();
                if(activeEditor instanceof MapViewer){
                    e.gc.setLineWidth(2);
                    e.gc.setForeground(new Color(null, 255, 0, 0));
                    e.gc.drawRectangle(rect);
                }
            }
        });
        canvas.addMouseListener(new MouseListener(){

            public void mouseDoubleClick(MouseEvent e){}

            public void mouseDown(MouseEvent e){
                IWorkbenchPage activePage = PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow().getActivePage();
                if(activePage == null){
                    return;
                }
                IEditorPart activeEditor = activePage.getActiveEditor();
                if(activeEditor instanceof MapViewer){
                    ((MapViewer)activeEditor).setShowRect(new Point(
                        (int)(e.x * scale), (int)(e.y * scale)));
                }
                mouseDown = true;
                canvas.setCursor(new Cursor(null, SWT.CURSOR_CROSS));
            }

            public void mouseUp(MouseEvent e){
                mouseDown = false;
                canvas.setCursor(new Cursor(null, SWT.CURSOR_ARROW));

            }
        });
        canvas.addMouseMoveListener(new MouseMoveListener(){

            public void mouseMove(MouseEvent e){
                if(mouseDown){
                    IWorkbenchPage activePage = PlatformUI.getWorkbench()
                        .getActiveWorkbenchWindow().getActivePage();
                    if(activePage == null){
                        return;
                    }
                    IEditorPart activeEditor = activePage.getActiveEditor();
                    if(activeEditor instanceof MapViewer){
                        ((MapViewer)activeEditor).setShowRect(new Point(
                            (int)(e.x * scale), (int)(e.y * scale)));
                    }
                }
            }
        });
    }

    @Override
    public void setFocus(){
        canvas.setFocus();
    }
}
